from PySide2.QtWidgets import QComboBox
from PySide2.QtCore import Qt
from PySide2.QtCharts import QtCharts

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt

from public import PublicData

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.ioff()

public_data = PublicData()


def show_bend_plt():
    sensor_dict = public_data.sensor_dic
    bend_data_list = public_data.bend_agree_list
    figure = plt.figure()
    canvas = FigureCanvas(figure)
    data_count = len(bend_data_list)

    x_range = get_x_value(data_count)
    x = [i for i in range(1, x_range+1)]
    alam_value = public_data.agree_max
    alarm_y = [alam_value for i in range(1, x_range+1)]
    plt.plot(x, alarm_y, lw=4, ls="-", c="red", label="警戒值")
    col_name_list = ["警戒值"]

    for senor in sensor_dict.keys():

        y_value = senor.agree_data_list
        x_len = get_x_value(y_value)

        x = [i for i in range(1,x_len+1)]
        label = senor.location
        col_name_list.append(label)
        plt.plot(x, y_value, lw=2, label=label)

    plt.legend(col_name_list)
    plt.title("角度值变化曲线")
    plt.ylabel("角度值：°")
    canvas.draw()
    return canvas


def get_x_value(data_count):
    data_len = len(str(data_count))
    return 10 ** (data_len - 1)


class ComboBox(QComboBox):
    def __init__(self, item_list):
        super().__init__()
        self.addItems(item_list)

    def get_value(self):
        return self.currentText()


class Chart(QtCharts.QChart):
    def __init__(self, title, y_range):
        super().__init__()
        self.setTitle(title)

        xAxis = QtCharts.QValueAxis()
        yAxis = QtCharts.QValueAxis()
        yAxis.setRange(*y_range)  # 设置刻度的范围
        yAxis.setTickCount(20)  # 设置刻度的数量

        self.addAxis(yAxis, Qt.AlignLeft)
